"""
Case Type   : resource pool
Case Name   : 创建资源池指定已创建的子class控制组,部分测试点合理报错
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.创建子class控制组,CPU资源配额20%
    2.创建资源池指定已创建的子class控制组class_a
    3.创建资源池指定子class控制组class_a已被资源池关联
    4.环境清理
Expect      :
    1.创建成功
    2.创建成功
    3.创建失败，合理报错
    4.清理成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.class_name = 'class_a'
        self.respool_1 = 'resp_resource_pool_0020_1'
        self.respool_2 = 'resp_resource_pool_0020_2'

    def test_resource_pool(self):
        text = "-----step1:创建子class控制组,CPU资源配额20%;expect:创建成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -c -S {self.class_name} -s 20;" \
              f"gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(f"CLASS  Name: {self.class_name}", cmd_res,
                      f'执行失败:{text}')

        text = "-----step2:创建资源池指定已创建的子class控制组class_a;expect:创建成功-----"
        self.log.info(text)
        sql = f'''drop resource pool if exists {self.respool_1};
        create resource pool {self.respool_1} 
        with (control_group = '{self.class_name}');
        select respool_name, control_group from pg_resource_pool 
        where respool_name = '{self.respool_1}';'''
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.create_resource_pool_success, sql_res,
                      f'执行失败:{text}')
        self.assertIn(f'{self.respool_1} | {self.class_name}', sql_res,
                      f'执行失败:{text}')

        text = "-----step3:创建资源池指定子class控制组class_a已被资源池关联;" \
               "expect:创建失败，合理报错-----"
        self.log.info(text)
        sql = f'''drop resource pool if exists {self.respool_2};
                create resource pool {self.respool_2} 
                with (control_group = '{self.class_name}');
                select respool_name, control_group from pg_resource_pool 
                where respool_name = '{self.respool_2}';'''
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn('ERROR:  resource pool with control_group class_a has '
                      'been existed in the two-layer resource pool list',
                      sql_res, f'执行失败:{text}')

    def tearDown(self):
        text = "-----step4:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop resource pool if exists {self.respool_1};"
            f"drop resource pool if exists {self.respool_2};")
        self.log.info(drop_res)
        cmd = f"source {macro.DB_ENV_PATH};gs_cgroup --revert"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.assertEqual(2, drop_res.count(
            self.constant.drop_resource_pool_success),
                         f'执行失败:{text}')
        self.assertEqual('', cmd_res, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
